package com.piesat.quake.business.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.piesat.quake.business.device.mapper.DNetworkDataMapper;
import com.piesat.quake.business.device.model.DeviceOptions;
import com.piesat.quake.business.device.model.NetworkData;
import com.piesat.quake.business.device.model.config.network.FtpConfig;
import com.piesat.quake.business.device.model.config.network.MqttConfig;
import com.piesat.quake.business.device.model.config.network.UpdateConfig;
import com.piesat.quake.business.device.service.DNetworkDataService;

import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-03-13
 */
@Service
public class NetworkDataServiceImpl extends ServiceImpl<DNetworkDataMapper, NetworkData> implements DNetworkDataService {


    @Override
    public NetworkData getConfig(String id, Byte type) {
        return baseMapper.getConfig(id,type);
    }

    @Override
    public NetworkData saveMqttConfig(MqttConfig config) {
        NetworkData networkData = new NetworkData();
        networkData.setAddress(config.getAddress());
        networkData.setPort(config.getPort());
        networkData.setUsername(config.getUsername());
        networkData.setPassword(config.getPassword());
        boolean b = baseMapper.insert(networkData) > 0;
        if (!b) {
            throw new IllegalArgumentException("保存mqtt配置失败");
        }
        return networkData;
    }

    @Override
    public NetworkData saveFtpConfig(FtpConfig config) {
        NetworkData networkData = new NetworkData();
        networkData.setAddress(config.getAddress());
        networkData.setPort(config.getPort());
        networkData.setUsername(config.getUsername());
        networkData.setPassword(config.getPassword());
        networkData.setPath(config.getPath());
        boolean b = baseMapper.insert(networkData) > 0;
        if (!b) {
            throw new IllegalArgumentException("保存ftp配置失败");
        }
        return networkData;
    }

    @Override
    public NetworkData saveUpdateConfig(UpdateConfig config) {
        NetworkData networkData = new NetworkData();
        networkData.setAddress(config.getAddress());
        networkData.setPort(config.getPort());
        boolean b = baseMapper.insert(networkData) > 0;
        if (!b) {
            throw new IllegalArgumentException("保存更新服务配置失败");
        }
        return networkData;
    }

    @Override
    public void update(NetworkData networkData) {
        QueryWrapper<NetworkData> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq("id", networkData.getId())
                .eq("type", networkData.getType());
        int update = baseMapper.update(networkData, updateWrapper);
        if (update == 0) {
            int insert = baseMapper.insert(networkData);
            if (insert == 0) {
                throw new IllegalStateException("修改网络参数失败");
            }
        }
    }

    @Override
    public void saveNetworkConfig(DeviceOptions deviceOptions) {
        NetworkData mqtt = deviceOptions.getMqttConfig().toNetWorkData();
        mqtt.setId(deviceOptions.getId());
        update(mqtt);
        NetworkData ftp = deviceOptions.getFtpConfig().toFtpNetWorkData();
        ftp.setId(deviceOptions.getId());
        update(ftp);
        NetworkData ftpLog = deviceOptions.getFtpLogConfig().toFtpLogNetWorkData();
        ftpLog.setId(deviceOptions.getId());
        update(ftpLog);
        NetworkData updateConfig = deviceOptions.getUpdateConfig().toNetWorkData();
        updateConfig.setId(deviceOptions.getId());
        update(updateConfig);
    }
}
